import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_API_URL, VITE_API_BASE_URL, VITE_APP_TITLE } = env;
  return {
    plugins: [vue()],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"), // 路径别名
      },
      extensions: [".js", ".ts", ".json"], // 导入时想要省略的扩展名列表
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import '/src/assets/styles/common.scss';`, // 全局变量
        },
      },
    },
    server: {
      host: "0.0.0.0",
      port: 3100,
      open: false,
      cors: true,
      proxy: {
        [VITE_API_BASE_URL]: {
          target: VITE_API_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(VITE_API_BASE_URL), ""), // 不可以省略rewrite
        },
      },
    },
  };
});
